package org.example.java_chatroom.service;



import org.example.java_chatroom.model.User;
import org.example.java_chatroom.model.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 获取用户的密保问题
     */
    /**
     * 获取用户的密保问题
     */
    public Map<String, Object> getSecurityQuestion(String username) {
        Map<String, Object> result = new HashMap<>();

        System.out.println("正在查询用户: " + username); // 添加日志

        User user = userMapper.selectByName(username);
        if (user == null) {
            System.out.println("用户不存在: " + username);
            result.put("success", false);
            result.put("message", "用户名不存在");
            return result;
        }

        System.out.println("找到用户: " + user.getUsername());
        System.out.println("密保问题: " + user.getSecurityQuestion());
        System.out.println("密保答案: " + user.getSecurityAnswer());

        // 注意：检查字段名是否匹配
        if (user.getSecurityQuestion() == null || user.getSecurityQuestion().trim().isEmpty()) {
            System.out.println("用户未设置密保问题");
            result.put("success", false);
            result.put("message", "该用户未设置密保问题");
            return result;
        }

        result.put("success", true);
        result.put("securityQuestion", user.getSecurityQuestion());
        System.out.println("返回密保问题: " + user.getSecurityQuestion());
        return result;
    }

    /**
     * 通过密保问题重置密码
     */
    public Map<String, Object> resetPasswordBySecurityQuestion(String username, String securityAnswer,
                                                               String newPassword, String confirmPassword) {
        Map<String, Object> result = new HashMap<>();

        // 验证两次密码是否一致
        if (!newPassword.equals(confirmPassword)) {
            result.put("success", false);
            result.put("message", "两次输入的密码不一致");
            return result;
        }

        // 验证密码长度
        if (newPassword.length() < 6) {
            result.put("success", false);
            result.put("message", "密码长度不能少于6位");
            return result;
        }

        // 验证用户信息
        User user = userMapper.selectByName(username);
        if (user == null) {
            result.put("success", false);
            result.put("message", "用户名不存在");
            return result;
        }

        // 验证密保答案
        if (user.getSecurityAnswer() == null || !user.getSecurityAnswer().equals(securityAnswer)) {
            result.put("success", false);
            result.put("message", "密保答案错误");
            return result;
        }

        try {
            // 更新密码 - 您需要在UserMapper中添加updatePassword方法
            userMapper.updatePassword(user.getUserId(), newPassword);

            result.put("success", true);
            result.put("message", "密码重置成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("message", "密码重置失败，请稍后重试");
        }

        return result;
    }

    /**
     * 设置或更新密保问题
     */
    public Map<String, Object> setSecurityQuestion(String username, String securityQuestion, String securityAnswer) {
        Map<String, Object> result = new HashMap<>();

        User user = userMapper.selectByName(username);
        if (user == null) {
            result.put("success", false);
            result.put("message", "用户名不存在");
            return result;
        }

        try {
            // 更新密保信息 - 您需要在UserMapper中添加updateSecurityInfo方法
            userMapper.updateSecurityInfo(user.getUserId(), securityQuestion, securityAnswer);
            result.put("success", true);
            result.put("message", "密保问题设置成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("message", "密保问题设置失败");
        }

        return result;
    }
}